package com.test.pudamoney.activity_fragment_pudamoney.fragment_pudamoney;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.test.pudamoney.bean_pudamoney.user_info_pudamoney.RepayInfo_pudamoney;
import com.test.pudamoney.constant_pudamoney.Constant_pudamoney;
import com.test.pudamoney.dialog_pudamoney.CustomerServiceDialog_pudamoney;
import com.test.pudamoney.util_pudamoney.StringUtil_pudamoney;
import com.test.pudamoney.util_pudamoney.http_pudamoney.HttpManager_pudamoney;
import com.test.pudamoney.R;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseActivity_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.base_pudamoney.BaseLoanFragment_pudamoney;
import com.test.pudamoney.activity_fragment_pudamoney.loan_pudamoney.RepayPaystackActivity_Niri_pudamoney;


public class RepayFragment_pudamoney extends BaseLoanFragment_pudamoney {

    private TextView totalRepaymentTv, dueDateTv;
    private TextView loanAmountTv, interestTv, serviceFeeTv, vatTv,overdueDaysTv, overduePenaltyTv;

    //展期还款控件
    private View planALayout,planBLayout;
    private ImageView planAIv,planBIv;
    private View planADetailLayout;
    private View planAFoldClickLayout;
    private ImageView planAFoldIv;
    private TextView extendedServiceChargesTv,daysOfExtensionTv;
    private TextView dueDateAfterExtension1Tv,dueDateAfterExtension2Tv;
    private TextView totalRepaymentDueTv;
    private TextView paymentAmountTv;

    //展期还款
    private boolean deferredFlag = false;

    //尼日线下还款
    private TextView offlineRepaymentTv_niri;
    private View offlineRepaymentView_niri;
    private TextView offlineRepaymentBankNameTv_niri, offlineRepaymentBankAccountTv_niri;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_repay_pudamoney, container, false);
        init(view);
        return view;
    }

    private void init(View view){

        totalRepaymentTv = view.findViewById(R.id.fragment_repay_tv_total_repayment);
        dueDateTv = view.findViewById(R.id.fragment_repay_tv_due_date);
        loanAmountTv = view.findViewById(R.id.fragment_repay_tv_loan_amount);
        interestTv = view.findViewById(R.id.fragment_repay_tv_interest);
        serviceFeeTv = view.findViewById(R.id.fragment_repay_tv_service_fee);
        overdueDaysTv = view.findViewById(R.id.fragment_repay_tv_overdue_days);
        overduePenaltyTv = view.findViewById(R.id.fragment_repay_tv_overdue_penalty);
        offlineRepaymentTv_niri = view.findViewById(R.id.fragment_repay_tv_offline_repayment_niri);
        offlineRepaymentView_niri = view.findViewById(R.id.fragment_repay_view_offline_repay_niri);
        ImageView copyBankNameIv_niri = view.findViewById(R.id.fragment_repay_iv_offline_repay_copy_bank_name_niri);
        ImageView copyBankAccountIv_niri = view.findViewById(R.id.fragment_repay_iv_offline_repay_copy_bank_account_niri);
        offlineRepaymentBankNameTv_niri = view.findViewById(R.id.fragment_repay_tv_offline_repay_bank_name_niri);
        offlineRepaymentBankAccountTv_niri = view.findViewById(R.id.fragment_repay_tv_offline_repay_bank_account_niri);
        //展期还款控件
        planALayout = view.findViewById(R.id.fragment_repay_layout_plan_a);
        planBLayout = view.findViewById(R.id.fragment_repay_layout_plan_b);
        planAIv = view.findViewById(R.id.fragment_repay_iv_plan_a);
        planBIv = view.findViewById(R.id.fragment_repay_iv_plan_b);
        planADetailLayout = view.findViewById(R.id.fragment_repay_layout_plan_a_detail);
        planAFoldClickLayout = view.findViewById(R.id.fragment_repay_layout_plan_a_fold_click);
        planAFoldIv = view.findViewById(R.id.fragment_repay_iv_plan_a_fold);
        extendedServiceChargesTv = view.findViewById(R.id.fragment_repay_tv_extended_service_charges);
        daysOfExtensionTv = view.findViewById(R.id.fragment_repay_tv_days_of_extension);
        dueDateAfterExtension1Tv = view.findViewById(R.id.fragment_repay_tv_due_date_after_extension_1);
        dueDateAfterExtension2Tv = view.findViewById(R.id.fragment_repay_plan_b_tv_due_date_after_extension_2);
        totalRepaymentDueTv = view.findViewById(R.id.fragment_repay_plan_b_tv_total_repayment_due);
        paymentAmountTv = view.findViewById(R.id.fragment_repay_tv_payment_amount);

        view.findViewById(R.id.fragment_repay_ib_customer_service).setOnClickListener(v -> new CustomerServiceDialog_pudamoney(requireContext()).show());

        view.findViewById(R.id.fragment_repay_btn_query_process).setOnClickListener(v -> HttpManager_pudamoney.getInstance().requestUserProcess());
        view.findViewById(R.id.fragment_repay_btn_repay).setOnClickListener(v -> {
            HttpManager_pudamoney.getInstance().saveUserBuriedPoint((this.getClass().getSimpleName()+"_CLICK").toUpperCase());

            HttpManager_pudamoney.getInstance().bankList_niri();

        });

        offlineRepaymentTv_niri.setVisibility(VISIBLE);
        offlineRepaymentTv_niri.setOnClickListener(v -> HttpManager_pudamoney.getInstance().createVirtualAccount());

        copyBankNameIv_niri.setOnClickListener(v -> {
            //获取剪贴板管理器：
            ClipboardManager cm = (ClipboardManager) requireContext().getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", RepayInfo_pudamoney.getInstance().getVirtualBankName());
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            Toast.makeText(requireContext(), getString(R.string.word_copy_success), Toast.LENGTH_SHORT).show();
        });

        copyBankAccountIv_niri.setOnClickListener(v -> {
            //获取剪贴板管理器：
            ClipboardManager cm = (ClipboardManager) requireContext().getSystemService(Context.CLIPBOARD_SERVICE);
            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", RepayInfo_pudamoney.getInstance().getVirtualBankAccount());
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            Toast.makeText(requireContext(), getString(R.string.word_copy_success), Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        HttpManager_pudamoney.getInstance().getRepayDetail();
    }

    @SuppressLint("SetTextI18n")
    public void updateRepayDetail(){

        //货币单位
        String moneyUnit = StringUtil_pudamoney.getSafeString(Constant_pudamoney.Money_Unit_Nigeria).toUpperCase();

        totalRepaymentTv.setText(moneyUnit+" "+ RepayInfo_pudamoney.getInstance().getRepayAmount());
        dueDateTv.setText(RepayInfo_pudamoney.getInstance().getRepayDate());
        loanAmountTv.setText(RepayInfo_pudamoney.getInstance().getAmount()+" "+moneyUnit);
        interestTv.setText(RepayInfo_pudamoney.getInstance().getInterest()+" "+moneyUnit);
        overdueDaysTv.setText(RepayInfo_pudamoney.getInstance().getOverdueDay()+" "+getString(R.string.word_days));
        overduePenaltyTv.setText(RepayInfo_pudamoney.getInstance().getOverdueAmount()+" "+moneyUnit);

        paymentAmountTv.setText(RepayInfo_pudamoney.getInstance().getRepayAmount()+" "+moneyUnit);

        //判断是否逾期
        if (RepayInfo_pudamoney.getInstance().isOverdue()){
            overdueDaysTv.setTextColor(Color.RED);
        }else {
            overdueDaysTv.setTextColor(requireContext().getColor(R.color.blue));
        }

        serviceFeeTv.setText(RepayInfo_pudamoney.getInstance().getServiceFee()+" "+moneyUnit);
        //线下还款
        if (!TextUtils.isEmpty(RepayInfo_pudamoney.getInstance().getVirtualBankAccount())) {
            offlineRepaymentView_niri.setVisibility(VISIBLE);
            offlineRepaymentTv_niri.setVisibility(GONE);
            offlineRepaymentBankNameTv_niri.setText(RepayInfo_pudamoney.getInstance().getVirtualBankName());
            offlineRepaymentBankAccountTv_niri.setText(RepayInfo_pudamoney.getInstance().getVirtualBankAccount());
        } else {
            offlineRepaymentView_niri.setVisibility(GONE);
            offlineRepaymentTv_niri.setVisibility(VISIBLE);
        }

        //展期还款
        if (RepayInfo_pudamoney.getInstance().getDeferredFlag()){
            planBLayout.setVisibility(VISIBLE);
        }else {
            planBLayout.setVisibility(GONE);
            //还款详情默认展开
            planADetailLayout.setVisibility(VISIBLE);
            planAFoldIv.setBackgroundResource(R.drawable.icon_repay_plan_a_unfolded);
        }

        planALayout.setOnClickListener(v -> setDeferredFlag(false));
        planBLayout.setOnClickListener(v -> setDeferredFlag(true));

        planAFoldClickLayout.setOnClickListener(v -> {
            if (planADetailLayout.getVisibility() == GONE){
                planADetailLayout.setVisibility(VISIBLE);
                planAFoldIv.setBackgroundResource(R.drawable.icon_repay_plan_a_unfolded);
            }else {
                planADetailLayout.setVisibility(GONE);
                planAFoldIv.setBackgroundResource(R.drawable.icon_repay_plan_a_folded);
            }
        });

        extendedServiceChargesTv.setText(RepayInfo_pudamoney.getInstance().getDeferredCurrentRepayAmount()+" "+moneyUnit);
        daysOfExtensionTv.setText(RepayInfo_pudamoney.getInstance().getDeferredDays()+" "+getString(R.string.word_days));
        dueDateAfterExtension1Tv.setText(RepayInfo_pudamoney.getInstance().getDeferredCurrentRepayTime());
        dueDateAfterExtension2Tv.setText(RepayInfo_pudamoney.getInstance().getDeferredRepayTime());
        totalRepaymentDueTv.setText(RepayInfo_pudamoney.getInstance().getDeferredRepayAmount()+" "+moneyUnit);
    }

    //展期选择
    @SuppressLint("SetTextI18n")
    private void setDeferredFlag(boolean deferredFlag) {

        this.deferredFlag = deferredFlag;
        if (deferredFlag){
            planAIv.setBackgroundResource(R.drawable.icon_item_check_false_1);
            planBIv.setBackgroundResource(R.drawable.icon_item_check_true_1);
            paymentAmountTv.setText(RepayInfo_pudamoney.getInstance().getDeferredCurrentRepayAmount()+" "+StringUtil_pudamoney.getSafeString(Constant_pudamoney.Money_Unit_Nigeria).toUpperCase());
        }else {
            planAIv.setBackgroundResource(R.drawable.icon_item_check_true_1);
            planBIv.setBackgroundResource(R.drawable.icon_item_check_false_1);
            paymentAmountTv.setText(RepayInfo_pudamoney.getInstance().getRepayAmount()+" "+StringUtil_pudamoney.getSafeString(Constant_pudamoney.Money_Unit_Nigeria).toUpperCase());
        }
    }

    public void onFetchBankListResponse_Niri(){
        //还款
        Context context = requireContext();
        if (context instanceof BaseActivity_pudamoney) ((BaseActivity_pudamoney)context).startActivity_pudamoney(RepayPaystackActivity_Niri_pudamoney.class);
    }

}